package com.cc.cbdb.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cc.cbdb.R;
import com.cc.cbdb.activity.X5WebViewActivity;
import com.cc.cbdb.adapter.base.BaseCommAdapter;
import com.cc.cbdb.adapter.base.ViewHolder;
import com.cc.cbdb.common.constants.Constant;
import com.cc.cbdb.common.report.ReportHelper;
import com.cc.cbdb.entity.QBiogCustom;
import com.cc.cbdb.common.utils.StringUtil;

import java.util.List;

/**
 * Created by tollcc on 2018/3/25.
 */

public class DataListAdapter extends BaseCommAdapter<QBiogCustom> {

    private Activity mActivity;
    private int mode = 0;
    public static boolean isSelect;

    public DataListAdapter(Activity context, List<QBiogCustom> datas) {
        super(datas);
        this.mActivity = context;
        if (!StringUtil.isEmpty(datas)){
            QBiogCustom qBiogCustom = datas.get(0);
            if (StringUtil.isEmpty(qBiogCustom.getAddrId())) {
                mode = 1;           //姓名查询
            }else {
                mode = 2;           //地址相关查询
            }
        }
    }

    @Override
    protected void setUI(ViewHolder holder, int position, Context context) {
        final QBiogCustom item = getItem(position);
        LinearLayout ll = holder.getItemView(R.id.ll);
        TextView name = holder.getItemView(R.id.name);
        TextView nameChn = holder.getItemView(R.id.name_chn);
        TextView dyTitle = holder.getItemView(R.id.dynasty_title);
        TextView dynasty = holder.getItemView(R.id.dynasty);
        TextView alnTitle =holder.getItemView(R.id.alert_name_title);
        TextView alertName = holder.getItemView(R.id.alert_name);
        TextView more = holder.getItemView(R.id.more);

        name.setText(item.getName());
        nameChn.setText(item.getNameChn());
        if (mode == 1){
            dyTitle.setText("朝代：");
            dynasty.setText(item.getDyChn());
            alnTitle.setText("别名：");
            alertName.setText(StringUtil.isEmpty(item.getAltName()) ? "无" : item.getAltName());
        }else {
            dyTitle.setText(item.getAddrDescChn()+"：");
            dynasty.setText(item.getAddrNameChn());
            alnTitle.setText("备注：");
            alertName.setText(StringUtil.isEmpty(item.getNotes()) ? "无" : item.getNotes());
            more.setVisibility(View.GONE);
        }

        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ReportHelper.getInstance(mActivity).reportQueryState(ReportHelper.PERSONINFO,item.getPersonId()+"",null);

                Intent intent;
                if (isSelect) {
                    intent = new Intent();
                    intent.putExtra("personId",item.getPersonId()+"");
                    intent.putExtra("param2",item.getNameChn());
                    mActivity.setResult(9,intent);
                    mActivity.finish();
                }else {
                    intent = new Intent(mActivity, X5WebViewActivity.class);
//                    StringBuilder sb = new StringBuilder(Constant.DOMAIN_PROJECT);
//                    sb.append(Constant.URL_QAPI);
//                    sb.append("?personId=");
//                    sb.append(item.getPersonId());
                    intent.putExtra("url",Constant.CBDBAPI+item.getPersonId());
                    intent.putExtra("title","人物资料");
                    mActivity.startActivity(intent);
                }
            }
        });

        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ReportHelper.getInstance(mActivity).reportQueryState(ReportHelper.CAREERCHANGE,item.getPersonId()+"",null);

                Intent intent = new Intent(mActivity, X5WebViewActivity.class);
                StringBuilder sb = new StringBuilder(Constant.DOMAIN_PROJECT);
                sb.append(Constant.HTML.HTML_QPOSTOFFTAB);
                sb.append("#");
                sb.append(item.getPersonId());
                intent.putExtra("url",sb.toString());
                intent.putExtra("title",item.getNameChn()+"的仕途");
                mActivity.startActivity(intent);
            }
        });
        
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_search_content;
    }
}
